home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / nasanets.zip / NETIO.H < prev    next >
Text File  |  1990-06-07  |  7KB  |  146 lines

  1.  
  2. /*=============================*/
  3. /*           NETS              */
  4. /*                             */
  5. /* a product of the AI Section */
  6. /* NASA, Johnson Space Center  */
  7. /*                             */
  8. /* principal author:           */
  9. /*       Paul Baffes           */
  10. /*                             */
  11. /* contributing authors:       */
  12. /*      Bryan Dulock           */
  13. /*      Chris Ortiz            */
  14. /*=============================*/
  15.  
  16.  
  17. /*
  18. ----------------------------------------------------------------------
  19.   NET I/O STRUCTURES MODULE  
  20. ----------------------------------------------------------------------
  21.   Much of the IO accomplished in this program requires some parsing and
  22.   some buffering (for speed).  Below, I've defined typical "token 
  23.   delimiters" for the parsing of the iopairs file.  Also, I have defined
  24.   a default buffer size for a buffer to hold io pairs (in Sint format) 
  25.   during the teaching process.  Finally, two file modes are defined so
  26.   that I may open files for both reading and writing.
  27. ----------------------------------------------------------------------
  28. */
  29.  
  30. #define SPACE           ' '     /* just some white space definitions */
  31. #define TAB             '\t'    /* for reading the input file        */
  32. #define NEWLINE         '\n'
  33. #define OPEN_PAREN      '('
  34. #define CLOSE_PAREN     ')'
  35. #define ENDSTRING       '\0'    /* special in 'C'; marks end of str  */
  36.  
  37. #define BUFFER_SIZE    1024     /* size, in bytes, used to create    */
  38.                                 /* the buffer for heavy IO (ie, for  */
  39.                                 /* reading io pairs & weights) 16384 */
  40.  
  41. #define READ_MODE       0       /* these two are used for creating   */       
  42. #define WRITE_MODE      1       /* and reading files (like "r", "w") */
  43.  
  44.  
  45. /*
  46. ----------------------------------------------------------------------
  47.   Now come some constants used while parsing through a layer spec. 
  48.   Layer specs consist of several commands (see 'IO_get_layer' routine)
  49.   and the idea here is to have a constant defined for each type of
  50.   command. 
  51. ----------------------------------------------------------------------
  52. */
  53. #define GLOBAL_LEARN_RATE  1
  54. #define GLOBAL_MOMENTUM    2
  55. #define LAYER              3
  56. #define NODES              4
  57. #define X_DIMENSION        5
  58. #define Y_DIMENSION        6
  59. #define LEARN_RATE         7
  60. #define SCALE_FACTOR       8
  61. #define MOMENTUM           9
  62. #define TARGET            10
  63. #define PATTERN_X_DIM     11
  64. #define PATTERN_Y_DIM     12
  65. #define X_OVERLAP         13
  66. #define Y_OVERLAP         14
  67.  
  68. #define LEARN_MAX         10.0
  69. #define SCALE_MAX          1.0
  70. #define MOMENTUM_MAX       2.0
  71.  
  72.  
  73. /*
  74. -------------------------------------------------------------------
  75.  Below is a structure which is convenient for manipulating the     
  76.   creation of a layer.  The idea is to read from a file and place  
  77.   the data into the above format.  Once this is done, other code   
  78.   will take the above structure and generate the actual Net layer  
  79.   corresponding to the data given.  This may seem like a waste of  
  80.   time; after all, why not just translate the data from the file   
  81.   directly into a Layer structure?  It turns out that it was much  
  82.   easier to handle the intermediate representation because of the  
  83.   recursive nature of the interdependence of Layers and Weights.   
  84.  Additionally, note that if it is ever decided that the input file 
  85.   should look drastically different, then all I need do is change  
  86.   the routines that read the file INTO THIS FORMAT.  Every other   
  87.   routine which depends upon this format will be fine.  Thus by    
  88.   separating the file format from my internal format, I save lots  
  89.   of future headaches which could occur due to changes in input    
  90.   file format.                                                     
  91. -------------------------------------------------------------------
  92. */
  93. typedef struct Layer_spec_str {
  94.    int    status;             /* indicates if EOF or ERROR occurred  */
  95.    int    ID;                 /* identification tag for the layer    */
  96.    int    num_nodes;          /* num nodes in the layer              */
  97.    int    X_dim;              /* The X-dimension of the layer        */
  98.    int    Y_dim;              /*  "  Y   "       "  "    "           */
  99.    float  learn_base;         /* used to hold the base learnng rate. */
  100.    float  learn_scale;        /* holds scaling factor, if present    */
  101.    float  momentum;           /* holds momemtum, if present          */
  102.    int    num_targets;        /* num of layers as target for wts     */
  103.    int16  targets[MAX_LAYERS][5]; 
  104.                               /* list of target layers and specs for */
  105.                               /* connection. [n][0]=target layer,    */
  106.                               /* [n][1] to [n][4] are pattern specs. */
  107.                               /* Each target may be either CONNECT   */
  108.                               /* ALL or PATTERNED. Iff patterened,   */
  109.                               /* then the last 4 specs of each entry */
  110.                               /* in this array represent the P,Q,R,  */
  111.                               /* and S specs of the pattern (see the */
  112.                               /* documentation under 'IO_get_layer') */
  113.                               /* Else, these last 4 = 0.             */
  114. } Layer_spec;
  115.  
  116.  
  117. /*
  118. -------------------------------------------------------------------
  119.  What follows is a structure used for speeding up the io during    
  120.   training of the net.  Since we have potentially large amounts of 
  121.   input-output pairs, it is really not feasible to read them all   
  122.   into memory at once.  Instead, we read in a large number of the  
  123.   inputs into a buffer and then do our io from the buffer.  If the 
  124.   buffer runs low, then our own customized "get" routines refill   
  125.   its contents.                                                    
  126.  A special piece of info is necessary to keep a check on the buffer
  127.   namely, we need to know where we currently are in the  buffer.   
  128.   We record this in the 'index' variable.                          
  129. -------------------------------------------------------------------
  130.  4-11-89  While upgrading NETS to handle both weights and bias values
  131.   I ran into a problem. The routine IO_get_from_workfile has no idea
  132.   how much of its buffer (below) actually contains good values. Here
  133.   I have added another element to the buffer structure to keep track of
  134.   the number of elements which are actually read in to the buffer.
  135.   Then, this value can be consulted to see if IO_get_from_workfile 
  136.   has exceed the last value somewhere in the middle of the buffer!
  137.   NOTE THAT LAST_ELEM IS AN INDEX ie, it indicates where the last 
  138.   valid entry is in the buffer.
  139. -------------------------------------------------------------------
  140. */
  141. typedef struct buffer_str {
  142.    Sint  values[BUFFER_SIZE];
  143.    int   index;
  144.    int   last_elem;
  145. } buffer;
  146.